package com.yihu.cms.action;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.yihu.article.beans.Article;
import com.yihu.article.beans.ArticleContent;
import com.yihu.article.beans.ArticleQuery;
import com.yihu.cms.service.ArticleService;
import com.yihu.cms.util.CommonUtils;
import com.yihu.cms.util.Osslog;

import net.sf.json.JSONObject;

@Controller
public class ArticleController extends BaseController{
	private final Logger logger = LoggerFactory.getLogger(ArticleController.class); 
	private ArticleService as = new ArticleService();
	
	@RequestMapping(value = "/article", method = RequestMethod.POST)
	public @ResponseBody Object add(@RequestBody Article article) {
		Osslog.debug("article.add.postTime: "+article.getPostTime());
		article.setType(1);
		int result = as.add(JSON.toJSONString(article));
		return CommonUtils.getResult(result, null);
	}
	
	//审核文章
	@RequestMapping(value = "/article/audit/{id}", method = RequestMethod.PUT)
	public @ResponseBody Object auditArticle(@PathVariable("id") String id) {
		return as.auditAticle(id);
	}
	
	@RequestMapping(value = "/article/{id}", method = RequestMethod.PUT)
	public @ResponseBody Object updateArticle(@RequestBody Article article,@PathVariable("id") String id) {
		logger.debug("id: "+id);
		article.setId(id);
		String json = JSON.toJSONString(article);
		logger.debug("json: "+json);
		int result = as.update(json);
		return CommonUtils.getResult(result, null);
	}
	
	@RequestMapping(value = "/article/{id}", method = RequestMethod.DELETE)
	public @ResponseBody Object deleteArticle(@PathVariable("id") String id) {
		System.out.println("ArticleController.delete");
		int result = as.delete(id);
		return CommonUtils.getResult(result, null);
	}
	
	@RequestMapping(value = "/article/list" , method = RequestMethod.GET)
	// , method = RequestMethod.PATCH
	public @ResponseBody Object getArticlePaging(ArticleQuery conditions) {
		
		String json = JSON.toJSONString(conditions);
		String resultJson= as.getArticlesPaging(json);
		return JSON.parse(resultJson);
	}
	
	@RequestMapping(value = "/article/{id}", method = RequestMethod.GET)
	public @ResponseBody Object getArticle(@PathVariable("id") String id) {
		logger.debug("ArticleController.getArticle11111111");
		String articleJson = as.getArticleById(id);
		return JSON.parseObject(articleJson, Article.class);
	}
	@RequestMapping(value = "/article/autoSave", method = RequestMethod.POST)
	public @ResponseBody Object autoSave(@RequestBody ArticleContent asVO) {
		logger.debug("autoSave： "+asVO.getArticleId());
		int result = as.autoSave(JSON.toJSONString(asVO));
		return CommonUtils.getResult(result, null);
	}
	
	@RequestMapping(value = "/article/getAutoContent", method = RequestMethod.POST)
	public @ResponseBody Object getAutoContent(@RequestBody ArticleContent asVO) {
		logger.debug("getAutoContent");
		ArticleContent content = as.getAutoCotent(JSON.toJSONString(asVO));
		return JSON.parse(JSON.toJSONString(content));
	}
}
